package com.ticket.sass.admin.dto.input.config;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.AssertTrue;
import javax.validation.constraints.NotBlank;

/**
 * @author ywb
 * @date 2024-04-28 11:34
 */
@Data
@ApiModel
public class WxPayConfigInput {
    @ApiModelProperty(value = "商户号", required = true)
    @NotBlank(message = "商户号不能为空")
    private String mchId;
    
    @ApiModelProperty(value = "商户密钥", required = true)
    @NotBlank(message = "商户密钥不能为空")
    private String mchKey;
    
    @ApiModelProperty(value = "是 修改 否 不修改")
    private Boolean fileUpdate = Boolean.FALSE;
    
    @ApiModelProperty(value = "p12证书内容")
    private MultipartFile file;
    
    @AssertTrue(message = "上传文件必须为p12格式")
    private boolean isFileType() {
        if (file == null) {
            return true; // 文件为空时不做校验
        }
        // 获取文件名
        String filename = file.getOriginalFilename();
        // 判断文件后缀是否为p12
        return filename != null && filename.toLowerCase().endsWith(".p12");
    }
    
    @AssertTrue(message = "上传文件大小不能超过1MB")
    private boolean isFileSize() {
        if (file == null) {
            return true; // 文件为空时不做校验
        }
        // 判断文件大小是否超过1MB
        return file.getSize() <= 1024 * 1023; // 1MB = 1024 * 1024 bytes
    }
    
}
